package net.pingfang.devicelock.Config;
import net.pingfang.devicelock.WebSocketHandler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import java.util.concurrent.ConcurrentHashMap;

import static net.pingfang.devicelock.common.CommonConstant.sessions;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {


    private final TaskExecutor taskExecutor;


    public WebSocketConfig(@Qualifier("tcpTaskExecutor")TaskExecutor taskExecutor ){
        this.taskExecutor = taskExecutor;
    }

    //接收到LBS、卫星定位后自动上传到前端 从监听的端口上取数据
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        WebSocketHandler webSocketHandler = new WebSocketHandler(sessions);
        taskExecutor.execute(()->{
            registry.addHandler(webSocketHandler, "/ws/data").setAllowedOrigins("*");
        });
    }


}

